package com.ebelter.btcomlib;


import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.text.TextUtils;

import com.ebelter.btcomlib.models.bluetooth.base.BlueManager;

import com.ebelter.btcomlib.models.bluetooth.products.dianji8.bean.Dj8Users;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.activity_manager.ActivityLifecycleCallbacksImp;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.tencent.bugly.crashreport.CrashReport;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;

public class CommonLib {
    public static Application APPContext;
    public static boolean isDebug = true;

    public static void init(Application context, String bugly_APPID) {

        APPContext = context;

        LogUtils.i("CommonLib","----PackageName:"+context.getPackageName());
        LogUtils.i("CommonLib","----当前手机使用的基本信息:"+getHandSetInfo(APPContext));

        closeAndroidPDialog();

        FlowManager.init(new FlowConfig.Builder(APPContext).build());//flowDb数据库初始化

        // Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());//捕获异常
        CrashReport.initCrashReport(context, bugly_APPID, true);

        context.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacksImp());//初始化界面管理器

        SpUtil.init(APPContext);//初始化两个sp工具 一个通用的  一个用户专业的

        UserSpUtil.init(APPContext);

        initNet();//初始化网络工具

        ToastUtil.init(APPContext);//初始化吐司

        BlueManager.getInstance().init(APPContext);//初始化蓝牙通用工具

        //AppUtils.isLocationOpen(context);


        Dj8Users.getInstance().clearUsers();

    }


    private static String getHandSetInfo(Context context){
        String handSetInfo=
            "手机型号:" + android.os.Build.MODEL +
            ",SDK版本:" + android.os.Build.VERSION.SDK +
             ",系统版本:" + android.os.Build.VERSION.RELEASE;
        return handSetInfo;

    }


    private static void closeAndroidPDialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //初始化网络
    private static void initNet() {
        OkGo.init(APPContext);
        OkGo.getInstance()
                // 打开该调试开关,打印级别INFO,并不是异常,是为了显眼,不需要就不要加入该行
                // 最后的true表示是否打印okgo的内部异常，一般打开方便调试错误
                .debug("OkGo", Level.INFO, true)
                //可以全局统一设置缓存模式,默认是不使用缓存,可以不传,具体其他模式看 github 介绍 https://github.com/jeasonlzy/
                .setCacheMode(CacheMode.NO_CACHE)
                .setConnectTimeout(6000)
                .setRetryCount(0);
        // .addCommonHeaders(getHttpCommonHeader()); //设置全局公共头
    }

    public static String getString(int res){
        return CommonLib.APPContext.getString(res);
    }

    // 是否开启定位服务
    public static boolean isLocServiceEnable(Context context) {
        LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        return gps || network;
    }
}
